<?php
/**
 * Created by JetBrains PhpStorm.
 * User: joedarkangel3
 * Date: 8/28/11
 * Time: 6:56 PM
 * To change this template use File | Settings | File Templates.
 */
 
class category_relation_test extends CI_Controller{

    function __construct()
    {
        parent::__construct();
        $dsn = 'mysql://root:c3603896@localhost/unit_test_yun';
        $this->load->database($dsn);
        $this->load->model('category_relation_model', 'category_relation');
        $this->load->library('unit_test');
        $this->unit->use_strict(true);
    }


    function index()
    {   
        // $food_category_id, $food_option_category_id, valid combo 6-2, 7-4, use 6,7 for food category id
        
        // create test
        $iv_create = $this->category_relation->create(999, 999);
        $this->unit->run(count($iv_create), 2, "invalid create attemp");

        $v_create1 = $this->category_relation->create(6, 2);
        $this->unit->run(count($v_create1['user']), 0, "valid creation");
        $duplicate_v_create = $this->category_relation->create(2, 3);
        $this->unit->run(array_key_exists('create_action', $duplicate_v_create['user']), true, "attempt to create duplicate relation");
        $v_create2 = $this->category_relation->create(7, 4);
        $this->unit->run(count($v_create2['user']), 0, "valid creation");

        // update test
        $this->category_relation->update(6, 4);
        $update_confirm = $this->category_relation->read(6);
        $this->unit->run($update_confirm['food_option_category_id'], "4", "update confirm");

        // delete test
        $this->category_relation->delete(6);
        $this->category_relation->delete(7);
        $delete_confirm = $this->category_relation->read(6);
        $this->unit->run(count($delete_confirm), 0, "delete confirm");

        // do report
        $this->_report();
    }

    private function _report()
    {
        if($this->input->is_ajax_request())
        {
            $final_result = $this->unit->result();
            $passed = 0;
            foreach($final_result as $res)
            {
                if($res['Result'] === "Passed")
                {
                    $passed++;
                }
            }
            if($passed == count($final_result))
            {
                $msg = json_encode(array('result' => 'passed'));
                echo $msg;
            }
            else{
                $msg = json_encode(array('result' => 'failed'));
                echo $msg;
            }
        }
        else{
            echo $this->unit->report();
        }
    }
}
